              package com.cadre.controller;


import java.util.List;


import javax.servlet.http.HttpServletRequest;


import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import com.cadre.entity.Message;
import com.cadre.entity.User;
import com.cadre.service.UserServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;



@Controller
public class UserController {
	@Autowired
    private UserServiceImpl userService;
	

	

		//村民登录
		@RequestMapping("/UserLogin")
		public String  login(User user,Model model,HttpServletRequest requset){
		User u = this.userService.login(user);		
			if(u!=null){
				System.out.println(u);
				 requset.getSession().setAttribute("user",u);
				
				 requset.getSession().setAttribute("status", "none");
				 
				 model.addAttribute("user",u);
			    	return "redirect:selectNews";
			}
			return "/LR.jsp";
			}
	
		//这里进行用户名验证
	
		@RequestMapping(value="/doCheckUsername")
		@ResponseBody
		public Message  registers(Model model,User user1,HttpServletRequest requset){
		String username= requset.getParameter("username");
		User user = this.userService.isexit(username);
		Message msg =null;
		
		if(user != null){
			 msg = new Message("no");	
	    	return msg;
		}
		
		if(user == null){
		
			if(!user1.getPassword().equals("") && !user1.getName().equals("") && !user1.getPhone().equals("") && !user1.getSex().equals("") && !user1.getUsername().equals("") && !user1.getAge().equals("")){
				boolean result = this.userService.register(user1);
				msg = new Message("yes");
				msg.setUser(user1);
				model.addAttribute("user1",user1);
				return msg;
			}else{	
				System.out.println("槽糕的程序");
			}
		}else{
			
}
		
		msg = new Message("def");
		return msg;	
	     }

		
		
		
		
		
		
		
		//-----------------------------------=
		//遍历所有村民
				@RequestMapping("/SelectAllUser")
				public String  SelectAllUser(User user,Model model,@RequestParam(required=true,defaultValue="1")int pageNum,@RequestParam(required=true,defaultValue="5") int pageSize,HttpServletRequest requset){
					PageHelper.startPage(pageNum,pageSize);
					List<User> list = this.userService.SelectAllUser();	
					PageInfo<User> page=new PageInfo<User>(list);
					model.addAttribute("page",page);
					 requset.getSession().setAttribute("statuss", "block");
					System.out.println(list);
					model.addAttribute("userlist",list);
					    return "/gl.jsp";
					}
			
				//删除村民
				@RequestMapping("/dodelete")
				public String  dodelete(Integer id,Model model){
					
					boolean isdelete = this.userService.deleteUserById(id);
					System.out.println("删除成功");
					if(isdelete){
						
						 return "forward:SelectAllUser";
					}else{
						return "/Error.jsp";
					}
			     }
			
				//更新前，需要先查找
				@RequestMapping("/toUpdateUser")
				public String selectMatterById(Integer id, Model model){	
				  User user = this.userService.SelectUserId(id);
					model.addAttribute("user", user);
					return "/CadreUpdateUser.jsp";
							}
				
				
				//更新村民
				@RequestMapping("/doUpdateUser")
				public String  UpdataUser(User user,Model model){
				boolean upuser = this.userService.UpdataUser(user);
					if(upuser){
						return "forward:SelectAllUser"; 
					}else{
						System.out.println("更新失败了");
						return "/gl.jsp";
					}
					    
					}
					//村干部添加村民信息
				/*@RequestMapping("/addUser")
				public String  addUser(User user,Model model){
				boolean upuser = this.userService.addUser(user);
					return "gl.jsp";
					    
					}*/
		/*
		* 下面是智铭的UserConller
				 */
				//搜索所有用户，表单显示
				@RequestMapping("/selectAllUser")
				public String selectUser(@RequestParam(required=true,defaultValue="1")int pageNum,@RequestParam(required=true,defaultValue="5")
				int pageSize,Model model,HttpServletRequest request){
					PageHelper.startPage(pageNum, pageSize);
					List<User> list = this.userService.selectAllUser();
					PageInfo<User> page=new PageInfo<User>(list);
					System.out.println("Controller层");
					model.addAttribute("users", list);
					model.addAttribute("page", page);
					
					
					return "/showUser.jsp";
				}
				//根据id删除选中用户
				@RequestMapping("/deleteUser")
				public String deleteUser(Model model,Integer id){
					boolean notice=this.userService.deleteUser(id);
					return "/selectAllUser";
					
				}
				//根据名字模糊搜索用户（村民）
				@RequestMapping("/selectUser1")
				public String selectUser1(@Param("name")@RequestParam(required=true,defaultValue="1")int pageNum,
		@RequestParam(required=true,defaultValue="5")int pageSize,String name,Model model,HttpServletRequest request){
					PageHelper.startPage(pageNum, pageSize);
					List<User> list = (List<User>) this.userService.selectUser1(name);
					PageInfo<User> page=new PageInfo<User>(list);
					System.out.println("Controller层");
					model.addAttribute("users", list);
					System.out.println(list);
					model.addAttribute("page", page);
					
					return "/MshowUser.jsp";
				}
//==========================================================================================		
				/*
				 * 下面是彭的UserConller
				 */
				//通过id查询用户
				@RequestMapping("selectUserById3")
				public String selectUserById3(@Param("id")Integer id,Model model) {
					User user = this.userService.selectUserById3(id);
					model.addAttribute("puser", user);
					return "showmyself2.jsp";
				}
				
				@RequestMapping("selectUserByIdToUpdate")
				public String selectUserByIdToUpdate(@Param("id")Integer id,Model model,HttpServletRequest request) {
					User u=(User)request.getSession(false).getAttribute("user");
					if(u!=null) {
//						User user = this.userServiceImpl.selectUserById(u.getId());
						model.addAttribute("user", u);
						return "showmyself.jsp";
					}else {
						return "showAll.jsp";
					}
				}
				
				//toupdate，修改前先查询，通过id
				@RequestMapping("toupdateUser")
				public String updateUser(Integer id,Model model) {
					User user = this.userService.selectUserById3(id);
					model.addAttribute("user", user);
					return "doupdate.jsp";
				}
				
				//查询出来后修改,doupdate
				@RequestMapping("doupdateUser")
				public String doupdateUser(User user,Model model,HttpServletRequest 

			request) {
					User use = (User)request.getSession(false).getAttribute("user");
					if(use!=null) {
						user.setId(use.getId());
						boolean result = this.userService.UpdataUser3(user);
						System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
						System.out.println(user);
						request.getSession().setAttribute("user",user);
						return "redirect:selectUserByIdToUpdate";
					}else {
						return "showAll.jsp";
					}
					
				}
				
				@RequestMapping("selectUsernameByUserid")
				public String selectUsernameByUserid(@Param("userid")Integer userid,Model model) {
					User user = this.userService.selectUsernameByUserid(userid); 
					System.out.println(user+"qqqqqqqqqqqqqqq");
					model.addAttribute("user1", user);
					return "issuelist.jsp";
				}
//				//toupdate，修改前先查询，通过id
//				@RequestMapping("toupdateUser")
//				public String updateUser(Integer id,Model model) {
//					User user = this.userService.selectUserById3(id);
//					model.addAttribute("user", user);
//					return "showmyself.jsp";
//				}
//				
//				//查询出来后修改,doupdate
//				@RequestMapping("doupdateUser3")
//				public String doupdateUser3(User user,Model model) {
//					boolean result = this.userService.updateUser3(user);
//					if(result) {
//						return "WEB-INF/showAll";//用以确认是否更改成功
//					}
//					return "showPeople";
//				}
//				
//				
//				
//				@RequestMapping("selectUsernameByUserid")
//				public String selectUsernameByUserid(@Param("userid")Integer userid,Model model) {
//					User user = this.userService.selectUsernameByUserid(userid); 
//					System.out.println(user+"qqqqqqqqqqqqqqq");
//					model.addAttribute("user1", user);
//					return "issuelist";
//				}
		
		
}
